package com.prjsystem.service.impl;

import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.prjsystem.common.constant.CMSConstant;
import com.prjsystem.common.util.CMSUtil;
import com.prjsystem.dao.CommonDao;
import com.prjsystem.dao.DailyWorkMstDao;
import com.prjsystem.dao.DailyWorkSearchDao;
import com.prjsystem.dao.WrInfoDao;
import com.prjsystem.entity.Daily;
import com.prjsystem.entity.DailyWorkBean;
import com.prjsystem.entity.UserInfo;
import com.prjsystem.service.DailyWorkService;

public class DailyWorkServiceImpl implements DailyWorkService{
	
	private DailyWorkSearchDao dailyWorkSearchDao;
	private DailyWorkBean dailyWorkBean = new DailyWorkBean();
	private Daily daily = new Daily();
	private WrInfoDao wrInfoDao;
	private CommonDao commonDao;
	private DailyWorkMstDao dailyWorkMstDao;
	/**
	 * DailyWork
	 */
	public DailyWorkBean rewrite(DailyWorkBean dailyWorkBean) {
		
		if (dailyWorkBean.getDwinfo() != null && dailyWorkBean.getDwinfo().size() > 0) {
			for(Daily daily:dailyWorkBean.getDwinfo()){
				if(daily.isChecked()){
					UserInfo userInfor = (UserInfo)ActionContext.getContext().getSession().get(CMSConstant.USERINFO);
					// 获取ID中的数据
					String sessionId = userInfor.getId();
					daily.setUserId(sessionId);
					dailyWorkMstDao.insertAllInfo(daily);
					wrInfoDao.updateDailyAllInfo();
				}
			}
			return dailyWorkBean;
		}
		return null;
	}
	
	public DailyWorkBean getDailyByDate(DailyWorkBean dailyWorkBean) {
		
		// TODO Auto-generated method stub
		UserInfo userInfor = (UserInfo)ActionContext.getContext().getSession().get(CMSConstant.USERINFO);
		// 获取ID中的数据
		String sessionId = userInfor.getId();
		String teamKey=userInfor.getTeamkey();
		daily.setSelectDate(dailyWorkBean.getDaily().getSelectDate());
		daily.setTeamKey(teamKey);
		daily.setUserId(sessionId);
		dailyWorkBean.setDaily(daily);
		dailyWorkBean.setDwinfo(dailyWorkMstDao.searchAllDailyByDate(daily));
		return dailyWorkBean;
	}
	
	public DailyWorkBean searchAllInfo(DailyWorkBean dailyWorkBean) {

		dailyWorkBean.setSelectListBean(commonDao.dropDowmList(CMSConstant.DAILY_WORK_DP_TYPE_SEARCH_LIST));
		dailyWorkBean.setDwinfo(dailyWorkSearchDao.searchDaily(dailyWorkBean));
		dailyWorkBean.setDwrinfo(dailyWorkSearchDao.searchdwrinfo(dailyWorkBean));
		return dailyWorkBean;
	}
	
	//selectList
	public DailyWorkBean searchSelectList(DailyWorkBean dailyWorkBean) {
		// 获取session中userInfo中的数据
		UserInfo userInfor = (UserInfo)ActionContext.getContext().getSession().get(CMSConstant.USERINFO);
		// 获取ID中的数据
		String sessionId = userInfor.getId();
		String teamKey = userInfor.getTeamkey();
		daily.setResourceId(sessionId);
		daily.setTeamNameId(teamKey);
		daily.setStartDate(CMSUtil.getCurrentDate());
		daily.setCost(CMSConstant.DailyWorkCost);
		daily.setRealHour(CMSConstant.DailyWorkRealHour);
		daily.setPercentage(CMSConstant.DailyWorkPercentage);
		dailyWorkBean.setDaily(daily);
		dailyWorkBean.setSelectListBean(commonDao.dropDowmList(CMSConstant.DAILY_WORK_DP_TYPE_DETAIL_LIST));
		return dailyWorkBean;
	}
	//delete all information of dailyWork include dailyWork,WorkRequest
	@Override
	public void deleteAllInfo (DailyWorkBean dailyWorkBean) {
		//search wrNo before delete dailyWork information
		for(Daily daily:dailyWorkBean.getDwinfo()){
			if(daily.isChecked())
			{
				List<Daily> infoWrNo = dailyWorkMstDao.searchwrNoBykey(daily.getId());
				if(infoWrNo.size()>0&&infoWrNo!=null){
					dailyWorkBean.setDaily(infoWrNo.get(0));
				}
				dailyWorkMstDao.deleteDialWorkInfoByKey(daily.getId());
				wrInfoDao.updateWrInfoByWrNo(dailyWorkBean.getDaily().getWrNo());
			}
		}
	}
	//search information of dailyWork by key before update dailyWork
	@Override
	public DailyWorkBean searchDailyWorkInfoByKey(DailyWorkBean dailyWorkBean) {
		for(Daily daily:dailyWorkBean.getDwinfo()){
			if(daily.isChecked()){
				List<Daily> list = dailyWorkMstDao.searchDailyByKey(daily.getId());
				if (list.size() > 0 && list!=null) {
					dailyWorkBean.setDaily(list.get(0));
				}
				break;
			}
		}
		dailyWorkBean.setSelectListBean(commonDao.dropDowmList(CMSConstant.DAILY_WORK_DP_TYPE_DETAIL_LIST));
		return dailyWorkBean;
	}
	@Override
	public void dailyMenu(DailyWorkBean dailyWorkBean) {
		
		if (!CMSUtil.isEmpty(dailyWorkBean.getDaily().getId())) {
			//update
			dailyWorkMstDao.updateDailyInfoDetail(dailyWorkBean.getDaily());
			wrInfoDao.updateWrInfoByWrNo(dailyWorkBean.getDaily().getWrNo());
		}
		else{
			//add
			dailyWorkMstDao.addDW(dailyWorkBean);
			wrInfoDao.updateWrInfoByWrNo(dailyWorkBean.getDaily().getWrNo());
		}
		
	}
	
	public void setDailyWorkSearchDao(DailyWorkSearchDao dailyWorkSearchDao) {
		this.dailyWorkSearchDao = dailyWorkSearchDao;
	}
	public DailyWorkSearchDao getDailyWorkSearchDao() {
		return dailyWorkSearchDao;
	}
	public WrInfoDao getWrInfoDao() {
		return wrInfoDao;
	}
	public void setWrInfoDao(WrInfoDao wrInfoDao) {
		this.wrInfoDao = wrInfoDao;
	}
	public DailyWorkMstDao getDailyWorkMstDao() {
		return dailyWorkMstDao;
	}
	public void setDailyWorkMstDao(DailyWorkMstDao dailyWorkMstDao) {
		this.dailyWorkMstDao = dailyWorkMstDao;
	}
	public CommonDao getCommonDao() {
		return commonDao;
	}
	public void setCommonDao(CommonDao commonDao) {
		this.commonDao = commonDao;
	}
	public void setDailyWorkBean(DailyWorkBean dailyWorkBean) {
		this.dailyWorkBean = dailyWorkBean;
	}
	public DailyWorkBean getDailyWorkBean() {
		return dailyWorkBean;
	}
	public Daily getDaily() {
		return daily;
	}
	public void setDaily(Daily daily) {
		this.daily = daily;
	}
}
